import axios from 'axios';
// import dayjs from 'dayjs';

const locales = ['en', 'ja', /* 'zh-Hans', */ 'zh-Hant'];

const getRoute = (path, priority) => {
  return locales.map((locale) => ({
    url: locale === 'en' ? path : `/${locale}${path === '/' ? '' : path}`,
    priority,
    changefreq: 'daily',
    lastmod: new Date().toISOString(),
    links: getLinks(path)
  }));
};

const getLinks = (path) => {
  const links = locales.map((locale) => ({
    lang: locale,
    url: locale === 'en' ? path : `/${locale}${path === '/' ? '' : path}`
  }));
  // return [{ lang: 'x-default', url: path }, ...links];
  return links;
};

const fetchCardetailRoutes = async () => {
  try {
    const response = await axios.post(
      `${process.env.NUXT_ENV_API_BASE_URL}/api/zuche/product/searchList`,
      {
        search_str: '',
        brand_id: '',
        pageNumber: 0,
        pageSize: 9999
      }
    );
    const carList = response.data?.data?.list || [];
    return carList.map((car) => ({ path: `/cardetail/${car.id}`, priority: 0.8 }));
  } catch (error) {
    console.log(`Failed to fetch cardetail routes`);
    return []; // 失败时返回空数组
  }
};

const fetchTourdetailRoutes = async () => {
  try {
    const response = await axios.post(
      `${process.env.NUXT_ENV_API_BASE_URL}/api/zuche/product/searchList`,
      {
        // start_time: dayjs().startOf('day').format('YYYY-MM-DD HH:mm'),
        // end_time: dayjs().startOf('day').format('YYYY-MM-DD HH:mm'),
        sort: '',
        language: 'en',
        sale_type: 'tour'
      }
    );
    const carList = response.data?.data?.list || [];
    return carList.map((car) => ({ path: `/tourdetail/${car.id}`, priority: 0.8 }));
  } catch (error) {
    console.log(`Failed to fetch tourdetail routes`);
    return []; // 失败时返回空数组
  }
};

const fetchArticaldetailRoutes = async () => {
  try {
    const response = await axios.post(
      `${process.env.NUXT_ENV_API_BASE_URL}/api/zuche/home/getArticleList`,
      {
        language: 'en', //
        type: 1, // 类型 1手册 2广告 3产品 4活动
        pageNumber: 1, // 当前页
        pageSize: 10 // 每页数量
      }
    );
    const list = response.data?.data?.list || [];
    return list.map((item) => ({ path: `/articaldetail/${item.id}`, priority: 0.8 }));
  } catch (error) {
    console.log(`Failed to fetch articaldetail routes`);
    return []; // 失败时返回空数组
  }
};

const fetchStoredetailRoutes = async () => {
  try {
    const response = await axios.post(
      `${process.env.NUXT_ENV_API_BASE_URL}/api/zuche/stores/getStoreListByLongLat`,
      {}
    );
    const list = response.data?.data || [];
    return list.map((item) => ({ path: `/shopdetail/${item.id}`, priority: 0.8 }));
  } catch (error) {
    console.log(`Failed to fetch shopdetail routes`);
    return []; // 失败时返回空数组
  }
};

export const fetchDynamicRoutesFromAPI = async () => {
  try {
    const routes = await Promise.all([
      fetchCardetailRoutes(),
      fetchArticaldetailRoutes(),
      fetchTourdetailRoutes(),
      fetchStoredetailRoutes()
    ]);
    return routes.flat().map((route) => getRoute(route.path, route.priority));
  } catch (error) {
    console.log(`Failed to fetch dynamic routes`);
    return []; // 失败时返回空数组
  }
};

export const getRoutes = async () => {
  const staticRoutes = [
    { path: '/', priority: 1.0 },
    { path: '/tour', priority: 0.9 },
    { path: '/shop', priority: 0.9 },
    { path: '/searchcar', priority: 0.9 },
    { path: '/board', priority: 0.9 }
  ].map((route) => getRoute(route.path, route.priority));

  const dynamicRoutes = await fetchDynamicRoutesFromAPI();

  return [...staticRoutes, ...dynamicRoutes].flat();
};
